cmake_minimum_required(VERSION 2.8)


#######################
# 项目
#######################
PROJECT(RGBDSLAM)
SET(PROJECT_PREFIX rgbdslam)

#######################
# 程序的版本
#######################
SET(RGBDSLAM_MAJOR_VERSION 0)
SET(RGBDSLAM_MINOR_VERSION 0)
SET(RGBDSLAM_PATCH_VERSION 1)

SET(PROJECT_VERSION_MAJOR ${RGBDSLAM_MAJOR_VERSION})
SET(PROJECT_VERSION_MINOR ${RGBDSLAM_MINOR_VERSION})
SET(PROJECT_VERSION_PATCH ${RGBDSLAM_PATCH_VERSION})

SET(RGBDSLAM_VERSION
  ${RGBDSLAM_MAJOR_VERSION}.${RGBDSLAM_MINOR_VERSION}.${RGBDSLAM_PATCH_VERSION})
  
set(CMAKE_MODULE_PATH 
  ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/modules") 

find_package(OpenNI2 REQUIRED)
find_package(OpenCV REQUIRED)
find_package(PCL 1.7 REQUIRED)
find_package(Eigen3 REQUIRED)

##依赖项的状态
MESSAGE("===========================")
if(OPENNI2_FOUND) 
    MESSAGE("--OpenNI2 found: Yes")
elseif(OPENNI2_FOUND)
	MESSAGE("--OpenNI2 found: No")
endif(OPENNI2_FOUND)

if(OPENCV_FOUND)
	MESSAGE("--opencv found: Yes")
elseif(OPENCV_FOUND)
	MESSAGE("--OpenCV found: No")
endif(OPENCV_FOUND)

if(PCL_FOUND)
	MESSAGE("--PCL found: Yes")
elseif(PCL_FOUND)
	MESSAGE("--PCL found: No")
endif(PCL_FOUND)

IF(EIGEN3_FOUND)
	MESSAGE("--Eigen3 found: Yes")
ELSEIF(EIGEN3_FOUND)
	MESSAGE("--Eigen3 found: No")
ENDIF(EIGEN3_FOUND)
MESSAGE("===========================")

####### 输出路径 #######
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)

####### OSX BUNDLE CMAKE_INSTALL_PREFIX #######
IF(APPLE AND BUILD_AS_BUNDLE)
  IF(Qt5_FOUND OR (QT4_FOUND AND QT_QTCORE_FOUND AND QT_QTGUI_FOUND))
       
    # Required when packaging, and set CMAKE_INSTALL_PREFIX to "/".
    SET(CMAKE_INSTALL_PREFIX "/")
    SET(CPACK_SET_DESTDIR TRUE) 
    
    SET(CMAKE_BUNDLE_NAME "${PROJECT_NAME}")
    SET(CMAKE_BUNDLE_LOCATION "${CMAKE_INSTALL_PREFIX}")
    
    # make sure CMAKE_INSTALL_PREFIX ends in /
    STRING(LENGTH "${CMAKE_INSTALL_PREFIX}" LEN)
    MATH(EXPR LEN "${LEN} -1" )
    STRING(SUBSTRING "${CMAKE_INSTALL_PREFIX}" ${LEN} 1 ENDCH)
    IF(NOT "${ENDCH}" STREQUAL "/")
      SET(CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}/")
    ENDIF(NOT "${ENDCH}" STREQUAL "/")
    SET(CMAKE_INSTALL_PREFIX 
      "${CMAKE_INSTALL_PREFIX}${CMAKE_BUNDLE_NAME}.app/Contents")
  ELSE()
    
    #If Qt is not here, no need to build a bundle
    SET(BUILD_AS_BUNDLE OFF)
  ENDIF()
ENDIF(APPLE AND BUILD_AS_BUNDLE)

####### 安装路径 #######
set(INSTALL_INCLUDE_DIR include/${PROJECT_PREFIX}-${RTABMAP_MAJOR_VERSION}.${RTABMAP_MINOR_VERSION} CACHE PATH
  "Installation directory for header files")
  
if(WIN32 AND NOT CYGWIN)
  set(DEF_INSTALL_CMAKE_DIR CMake)
else()
  set(DEF_INSTALL_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/${PROJECT_PREFIX}-${RTABMAP_MAJOR_VERSION}.${RTABMAP_MINOR_VERSION})
endif()
set(INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH
  "Installation directory for CMake files")
  
ADD_SUBDIRECTORY(utilite)
ADD_SUBDIRECTORY(corelib)
ADD_SUBDIRECTORY(guilib)
ADD_SUBDIRECTORY(app)
ADD_SUBDIRECTORY(examples)

#########################
# Setup RGBDSLAM.cmake
#########################

# Create the RGBDSLAM.cmake and RGBDSLAMConfigVersion files
file(RELATIVE_PATH REL_INCLUDE_DIR "${CMAKE_INSTALL_PREFIX}/${INSTALL_CMAKE_DIR}" "${CMAKE_INSTALL_PREFIX}/${INSTALL_INCLUDE_DIR}")
file(RELATIVE_PATH REL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/${INSTALL_CMAKE_DIR}" "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")

SET(CONF_INCLUDE_DIRS "${PROJECT_SOURCE_DIR}/corelib/include"
  "${PROJECT_SOURCE_DIR}/utilite/include"
)

SET(CONF_LIB_DIR "${CMAKE_ARCHIVE_OUTPUT_DIRECTORY} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")

#######################
# CPack
#######################
INCLUDE(InstallRequiredSystemLibraries)
MESSAGE(${PROJECT_NAME})
SET(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
SET(CPACK_PACKAGE_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}")  
SET(CPACK_PACKAGE_VERSION_MINOR "${PROJECT_VERSION_MINOR}")
SET(CPACK_PACKAGE_VERSION_PATCH "${PROJECT_VERSION_PATCH}")

SET(CPACK_SOURCE_IGNORE_FILES 
  "\\\\.svn/" 
  "\\\\.settings/" 
  "${PROJECT_SOURCE_DIR}/build/[a-zA-Z0-9_]+" 
  "~$" 
  "${PROJECT_SOURCE_DIR}/bin/.*${PROJECT_PREFIX}"
  "${PROJECT_SOURCE_DIR}/bin/.*${PROJECT_NAME}"
  "${PROJECT_SOURCE_DIR}/bin/.*[tT]est"
  "${PROJECT_SOURCE_DIR}/bin/.*[eE]xample"
  "${PROJECT_SOURCE_DIR}/bin/.*uresourcegenerator"
  "\\\\.DS_Store"
)

#使用cmake.in配置文件生成cmake文件
configure_file(RGBDSLAMConfig.cmake.in
  "${PROJECT_BINARY_DIR}/RGBDSLAMConfig.cmake" @ONLY)

# ... for both
configure_file(RGBDSLAMConfigVersion.cmake.in
  "${PROJECT_BINARY_DIR}/RGBDSLAMConfigVersion.cmake" @ONLY)

# ... for the install tree
set(CONF_INCLUDE_DIRS "\${RGBDSLAM_CMAKE_DIR}/${REL_INCLUDE_DIR}")
set(CONF_LIB_DIR "\${RGBDSLAM_CMAKE_DIR}/${REL_LIB_DIR}")
configure_file(RGBDSLAMConfig.cmake.in
  "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/RGBDSLAMConfig.cmake" @ONLY)

  # Install the RTABMapConfig.cmake and RTABMapConfigVersion.cmake
install(FILES
  "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/RGBDSLAMConfig.cmake"
  "${PROJECT_BINARY_DIR}/RGBDSLAMConfigVersion.cmake"
  DESTINATION "${INSTALL_CMAKE_DIR}" COMPONENT devel)
####

### Install package.xml for catkin
#install(FILES package.xml DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_PREFIX}")


INCLUDE(CPack)



